package sv.com.seguros.web.controller.asg;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import sv.com.seguros.dto.asg.AsgReclamo;
import sv.com.seguros.exception.WebException;
import sv.com.seguros.web.controller.BaseController;
import sv.com.seguros.web.util.DBContext;
import sv.com.seguros.web.util.WebUtilidades;

@Controller
@RequestMapping("/reclamos")
public class AsgReclamoController extends BaseController {

	private static final long serialVersionUID = 3834669882649782965L;
	
	@RequestMapping(value = "/view", method = {RequestMethod.GET, RequestMethod.POST})
	public String view(ModelMap model) {
		model.put(WebUtilidades.HEADER_TITLE, "RECLAMOS");
		try {
			model.put("ctgTipoContactos", JSONArray.fromObject(DBContext.getCtgCatalogoDao().findAllByCodigoAsArray(WebUtilidades.CTG_TIPO_CONTANTO_PADRE)));
			model.put("isReadOnly", WebUtilidades.isReadOnly(WebUtilidades.ROLE_RECLAMO));
			model.put("allowDelete", WebUtilidades.isAllowDelete(WebUtilidades.ROLE_RECLAMO));
		} catch (WebException e) {
			logger.error("Exception:", e);
		}
		return "aseguradora/reclamo/asgDetalleReclamo";
	}
	
	@RequestMapping(value = "/save", method = {RequestMethod.POST})
	public void save(
			@ModelAttribute("asgReclamo") AsgReclamo asgReclamo, 
			HttpServletResponse response){
		Map<String, Object> result = new HashMap<String, Object>();
		try{
			asgReclamo.setSgdUsuario(WebUtilidades.getSgdUsuarioFromAcegi());
			DBContext.getAsgReclamoService().save(asgReclamo);
			result.put("reclamos", DBContext.getAsgReclamoDao().findAllByAsgPolizaId(asgReclamo.getAsgPoliza().getAsgPolizaId()));
			result.put(SUCCESS, true);
			result.put(MESSAGE, UPDATED_DATA);
		}catch(WebException e){
			logger.error("Exception:", e);
			result.put(FAILURE, true);
			result.put(MESSAGE, e.getExceptionMessage());
		}catch(Exception e){
			logger.error("Exception:", e);
			result.put(FAILURE, true);
			result.put(MESSAGE, DEFAULT_ERROR_MESSAGE);
		}
		serializeObject(result, response);
	}
	
	@RequestMapping(value = "/finalizar", method = {RequestMethod.POST})
	public void finalizar(
			@RequestParam Long asgReclamoId, 
			@RequestParam Long asgPolizaId, 
			HttpServletResponse response){
		Map<String, Object> result = new HashMap<String, Object>();
		try{
			DBContext.getAsgReclamoService().finalizar(asgReclamoId);
			result.put("reclamos", DBContext.getAsgReclamoDao().findAllByAsgPolizaId(asgPolizaId));
			result.put(SUCCESS, true);
			result.put(MESSAGE, UPDATED_DATA);
		}catch(WebException e){
			logger.error("Exception:", e);
			result.put(FAILURE, true);
			result.put(MESSAGE, e.getExceptionMessage());
		}catch(Exception e){
			logger.error("Exception:", e);
			result.put(FAILURE, true);
			result.put(MESSAGE, DEFAULT_ERROR_MESSAGE);
		}
		serializeObject(result, response);
	}
	
	@RequestMapping(value = "/findByAsgPolizaId", method = {RequestMethod.POST})
	public void findByAsgPolizaId(@RequestParam Long asgPolizaId, HttpServletResponse response){
		Map<String, Object> result = new HashMap<String, Object>();
		try{
			result.put("reclamos", DBContext.getAsgReclamoDao().findAllByAsgPolizaId(asgPolizaId));
			result.put(SUCCESS, true);
			result.put(MESSAGE, "Datos obtenidos");
		}catch(WebException e){
			logger.error("Exception:", e);
			result.put(FAILURE, true);
			result.put(MESSAGE, e.getExceptionMessage());
		}catch(Exception e){
			logger.error("Exception:", e);
			result.put(FAILURE, true);
			result.put(MESSAGE, DEFAULT_ERROR_MESSAGE);
		}
		serializeObject(result, response);
	}
	
	@RequestMapping(value = "/doSearch", method = {RequestMethod.POST})
	public void doSearch(HttpServletResponse response,
			@ModelAttribute AsgSeguroForm asgSeguroForm){
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			List<Object[]> list = DBContext.getAsgClienteDao().findForSeguimientoDetail(asgSeguroForm, "reclamo");
			result.put("totalResult", list.size());
			if(WebUtilidades.isEmptyList(list)) result.put("list", JSONArray.fromObject(list));
			else{
				result.put("list", JSONArray.fromObject(WebUtilidades.sublist(list, asgSeguroForm.getStart(), asgSeguroForm.getLimit())));
			}
		} catch (Exception e) {
			logger.error("Exception: ", e);
		}
		serializeObject(result, response);
	}
	
	@RequestMapping(value = "/findByType", method = {RequestMethod.POST})
	public void findByType(
			@RequestParam Long asgReclamoId,
			HttpServletResponse response){
		Map<String, Object> result = new HashMap<String, Object>();
		try{
			result.put("seguimientos", DBContext.getAsgSeguimientoDao().findAllByType(asgReclamoId, "reclamo"));
			result.put("contactos", DBContext.getAsgContactoDao().findAllByType(asgReclamoId, "reclamo"));
			result.put(SUCCESS, true);
			result.put(MESSAGE, "Datos obtenidos");
		}catch(WebException e){
			logger.error("Exception:", e);
			result.put(FAILURE, true);
			result.put(MESSAGE, e.getExceptionMessage());
		}catch(Exception e){
			logger.error("Exception:", e);
			result.put(FAILURE, true);
			result.put(MESSAGE, DEFAULT_ERROR_MESSAGE);
		}
		serializeObject(result, response);
	}

	@RequestMapping(value = "/deleteReclamo", method = {RequestMethod.GET, RequestMethod.POST})
	public void deleteReclamo(HttpServletResponse response, @RequestParam Long asgReclamoId, @RequestParam Long asgPolizaId) {
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			DBContext.getAsgReclamoService().deleteAsgReclamo(asgReclamoId);
			result.put("reclamos", DBContext.getAsgReclamoDao().findAllByAsgPolizaId(asgPolizaId));
			result.put(SUCCESS, true);
			result.put(MESSAGE, UPDATED_DATA);
		} catch (WebException e) {
			logger.error("Exception: ", e);
			result.put(FAILURE, true);
			result.put(MESSAGE, e.getExceptionMessage());
		} catch (Exception e) {
			logger.error("Exception: ", e);
			result.put(FAILURE, true);
			result.put(MESSAGE, DEFAULT_ERROR_MESSAGE);
		}
		serializeObject(result, response);
	}
}
